#pragma once

#include "winos.h"

_SSL_BEGIN
_SSWINOSL_BEGIN

_SSBASEL_USE_NP


class SSWINOS_CLASS win_proces
{
public:
	win_proces();
	virtual ~win_proces();

public:
	// Before Call create again, you must first call destroy() ro join() or detach() to Cleanup the prev state
	bool create(const tstring &command_line, unsigned short show_mode = SW_SHOW,
		const tstring &work_directory = NULL, bool use_std_handle = false,
		DWORD create_flags = 0, const tstring &desktop = _T(""));

	void destroy();
	bool join(unsigned long milli_seconds = INFINITE, unsigned long *exit_code = NULL);
	bool detach(
		HANDLE *process_h = NULL,
		HANDLE *stdin_h = NULL,
		HANDLE *stdout_h = NULL,
		HANDLE *stderr_h = NULL);

	bool wait(unsigned long milli_seconds = INFINITE);
	bool get_exitcode(unsigned long *exit_code);

	bool write(const void* buffer, unsigned long buffer_bytes, unsigned long *trans_bytes);
	bool read(void* buffer, unsigned long bytes_to_read, unsigned long *trans_bytes,
		unsigned long milli_seconds = INFINITE, bool use_std_eror_handle = false);

	HANDLE get_process_handle() const;
	HANDLE get_stderror_handle() const;
	HANDLE get_stdout_handle() const;

	bool is_runing();

private:
	HANDLE stdin_write_h_;
	HANDLE stdout_read_h_;
	HANDLE stderr_read_h_;
	HANDLE process_;
};

_SSWINOSL_END
_SSL_END
